package com.lianyu507.task;

import com.lianyu507.mapper.DtOperationLogMapper;
import com.lianyu507.util.SpringContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * =================================
 * <p>
 * Created by Administrator on 2017/11/21.
 * <p>
 * 描述:
 */

public class LogTask {

    private static Logger log = LoggerFactory.getLogger(LogTask.class);
    private static DtOperationLogMapper dtOperationLogMapper = SpringContextHolder.getBean(DtOperationLogMapper.class);

    public static void operationLog(Map<String, Object> map) {
        byte isadmin = (byte) map.get("isAdmin");
        Integer userId = (Integer) map.get("userId");
        String ip = (String) map.get("ip");
        byte referenceType = (byte) map.get("referenceType");
        Integer referenceId = (Integer) map.get("referenceId");
        String UserName = (String) map.get("userName");
        String operation = (String) map.get("operationLog");
        Date time = (Date) map.get("time");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s_time = format.format(time);
        if (referenceType == 0) {
            String message = "用户" + UserName + "在" + s_time + "时登录";
            dtOperationLogMapper.insert(CreateLog.createOperationLog(isadmin, userId, ip, message, referenceType, referenceId));
        }
        if (referenceType == 1) {
            String message = "用户" + UserName + "在" + s_time + "时登出";
            dtOperationLogMapper.insert(CreateLog.createOperationLog(isadmin, userId, ip, message, referenceType, referenceId));
        }
        if (referenceType == 2) {
            String message = "用户" + UserName + "在" + s_time + "时在cr_article表中" + operation + "id为" + referenceId + "的记录";
            dtOperationLogMapper.insert(CreateLog.createOperationLog(isadmin, userId, ip, message, referenceType, referenceId));
        }
        if (referenceType == 3) {
            String message = "用户" + UserName + "在" + s_time + "时在sh_user_word表中" + operation + "id为" + referenceId + "的记录";
            dtOperationLogMapper.insert(CreateLog.createOperationLog(isadmin, userId, ip, message, referenceType, referenceId));
        }
        if (referenceType == 4) {
            String message = "用户" + UserName + "在" + s_time + "时在cr_word_translate表中" + operation + "id为" + referenceId + "的记录";
            dtOperationLogMapper.insert(CreateLog.createOperationLog(isadmin, userId, ip, message, referenceType, referenceId));
        }
        if (referenceType == 5) {
            String message = "用户" + UserName + "在" + s_time + "时在mb_user表中" + operation + "id为" + referenceId + "的记录";
            dtOperationLogMapper.insert(CreateLog.createOperationLog(isadmin, userId, ip, message, referenceType, referenceId));
        }
        if (referenceType == 6) {
            String message = "用户" + UserName + "在" + s_time + "时在mb_manager表中" + operation + "id为" + referenceId + "的记录";
            dtOperationLogMapper.insert(CreateLog.createOperationLog(isadmin, userId, ip, message, referenceType, referenceId));
        }
    }
}
